package com.ruoyi.eservice.service.impl;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.eservice.pojo.dto.PolicyDto;
import com.ruoyi.eservice.pojo.entity.Cases;
import com.ruoyi.eservice.pojo.entity.Dict;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.base.BaseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.eservice.mapper.PolicyMapper;
import com.ruoyi.eservice.pojo.entity.Policy;
import com.ruoyi.eservice.service.IPolicyService;

/**
 * 区域的疫情政策信息Service业务层处理
 *
 * @author fencheng
 * @date 2022-11-10
 */
@Service
public class PolicyServiceImpl extends ServiceImpl<PolicyMapper,Policy> implements IPolicyService
{
    @Autowired
    private PolicyMapper policyMapper;

    @Autowired
    private DictServiceImpl dictService;

    public <T> QueryWrapper<T> createSelWrapper(Class<T> type) {
        QueryWrapper<T> queryWrapper = Wrappers.query();
        queryWrapper.eq("del_flag", "0");
        return queryWrapper;
    }

    /**
     * 查询区域的疫情政策信息
     *
     * @param policyId 区域的疫情政策信息主键
     * @return 区域的疫情政策信息
     */
    @Override
    public Policy selectPolicyByPolicyId(Long policyId)
    {
        return policyMapper.selectById(policyId);
    }

    /**
     * 查询区域的疫情政策信息列表
     *
     * @param policy 区域的疫情政策信息
     * @return 区域的疫情政策信息
     */
    @Override
    public List<Policy> selectPolicyList(Policy policy)
    {
        QueryWrapper<Policy> queryWrapper = createSelWrapper(Policy.class);
        queryWrapper.lambda()
                .eq(StringUtils.isNotNull(policy.getPolicyId()), Policy::getPolicyId, policy.getPolicyId())
                .eq(StringUtils.isNotNull(policy.getDictId()), Policy::getDictId, policy.getDictId())
                .eq(StringUtils.isNotEmpty(policy.getContent()), Policy::getContent, policy.getContent())
                .eq(StringUtils.isNotEmpty(policy.getDelFlag()), Policy::getDelFlag, policy.getDelFlag())
        ;
        return policyMapper.selectList(queryWrapper);
    }

    /**
     * 新增区域的疫情政策信息
     *
     * @param policy 区域的疫情政策信息
     * @return 结果
     */
    @Override
    public int insertPolicy(Policy policy)
    {
        policy.setCreateBy(SecurityUtils.getUsername());
        policy.setCreateTime(DateUtils.getNowDate());
        policy.setUpdateBy(SecurityUtils.getUsername());
        policy.setUpdateTime(DateUtils.getNowDate());
        int rows = policyMapper.insert(policy);
        return rows;
    }

    /**
     * 修改区域的疫情政策信息
     *
     * @param policy 区域的疫情政策信息
     * @return 结果
     */
    @Override
    public int updatePolicy(Policy policy)
    {
        policy.setUpdateBy(SecurityUtils.getUsername());
        policy.setUpdateTime(DateUtils.getNowDate());

        return policyMapper.updateById(policy);
    }

    /**
     * 批量删除区域的疫情政策信息
     *
     * @param policyIds 需要删除的区域的疫情政策信息主键
     * @return 结果
     */
    @Override
    public int deletePolicyByPolicyIds(Long[] policyIds)
    {
        return policyMapper.deleteBatchIds(Arrays.asList(policyIds));
    }

    /**
     * 删除区域的疫情政策信息信息
     *
     * @param policyId 区域的疫情政策信息主键
     * @return 结果
     */
    @Override
    public int deletePolicyByPolicyId(Long policyId)
    {
        return policyMapper.deleteById(policyId);
    }


    @Override
    public AjaxResult getPolicyByName(String areaname) {
        Dict one = null;
        System.out.println(areaname);

        try {
            //根据传来地区的名字查询该地区的具体信息
            one = dictService.lambdaQuery().likeRight(Dict::getName, areaname).one();
        } catch (Exception e) {
            //查询到多个项目抛出异常
            return AjaxResult.error("查询到多个符合项目，请精确查询条件！");
        }

        //没有查询到数据
        if (one == null) {
            return AjaxResult.error("没有查询到该地点，请精确查询条件！");
        }

        //获取地域id
        Long dictId = one.getDictId();
        //获得当前地域的级别
        Long level = one.getLevel();
        System.out.println(level);
        //查询到区级
        if (level == 3) {
            List<Policy> list = lambdaQuery().eq(Policy::getDictId, dictId).list();
            return AjaxResult.success("获取区级病例信息成功", list);
        }

        //查询到市级
        if (level == 2) {
            //查询市下的所有区
            List<Dict> list = dictService.lambdaQuery().eq(Dict::getParentId, dictId).list();

            PolicyDto policyDto = new PolicyDto();
            policyDto.setPolicyId(dictId);

            if (list.isEmpty()) {
                return AjaxResult.error("没有查询到该市的信息");
            }

            //遍历查询每个区的信息
            list.stream().forEach(li -> {
                //获取区所有政策
                List<Policy> policylist = lambdaQuery().eq(Policy::getDictId, li.getDictId()).list();

                if(!policylist.isEmpty()){
                    policylist.stream().forEach(pl -> {
                        //将政策封装到实体类
                        policyDto.addPolicy(pl);
                    });
                }
            });
            return AjaxResult.success("获取市级病例信息成功", policyDto);
        }

        //查询到省级
        if (level == 1) {

            //查询省下面的市列表
            List<Dict> list = dictService.lambdaQuery().eq(Dict::getParentId, dictId).list();
            PolicyDto policyDto = new PolicyDto();
            policyDto.setPolicyId(dictId);


            //没有市数据
            if (list.isEmpty()) {
                return AjaxResult.error("没有查询到该省的数据！");
            }

            //获取当前市内的区信息
            list.stream().forEach(li -> {
                //获得当前区的列表
                List<Dict> areaList = dictService.lambdaQuery().eq(Dict::getParentId, li.getDictId()).list();

                //该城市所有区域信息
                if (!areaList.isEmpty()) {
                    areaList.stream().forEach(al -> {
                        //获取当前区的所有政策数据
                        List<Policy> policyList = lambdaQuery().eq(Policy::getDictId, al.getDictId()).list();
                        if(!policyList.isEmpty()){
                            policyList.stream().forEach(pl -> {
                                policyDto.addPolicy(pl);
                            });
                        }
                    });
                }
            });

            return AjaxResult.success("获取省级病例信息成功！", policyDto);
        }
        return AjaxResult.error("没有查询到匹配的地区！");
    }
}
